# Do Gains in Political Representation Sweeten Tax Reform in China? 
# It Depends on Who You Ask? 

#### Political Science Research and Methods*
#### This R-script replicates Figure A-13
#### Comparison of a set of govt. service by elite status

rm(list=ls())
setwd("/Users/JayCK/Library/CloudStorage/Box-Box/VAT_Project_New/Replication")


library(tidyverse)
library(estimatr)
library(haven)
library(ggsignif)
library(ggpval)
library(ggpubr)
library(cowplot)
theme_set(theme_bw())

# # Round values to two decimal points
round_df <- function(x, digits) {
  numeric_columns <- sapply(x, mode) == 'numeric'
  x[numeric_columns] <-  round(x[numeric_columns], digits)
  x
}

# Prep Data Frame
public.goods <- read_dta("china_ntwr.dta")
public.goods <- tbl_df(public.goods)

public.goods <- public.goods %>%
  mutate(elite_t10 = case_when(
    elite_t10 == 0 ~ 'Nonelites',
    elite_t10 == 1 ~ 'Elites')) %>%
  filter(profile==1 & task==1)

################
# Satisfaction with education 
################

education_satisfaction <-
  public.goods %>% group_by(elite_t10) %>%
  do(tidy(lm_robust(edu_sat_dummy ~ 1, data = .))) %>%
  mutate(edu_sat_dummy = estimate) %>%
  filter(elite_t10!="NA")

education_satisfaction <- round_df(education_satisfaction, 2)

ggplot(education_satisfaction, aes(x=elite_t10, y=edu_sat_dummy)) + 
  geom_bar(stat="identity", width=0.8, fill=c('gray45','gray69'),
           position=position_dodge()) +
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high), width=.2,
                position=position_dodge(.9)) +
  scale_y_continuous(expand = c(0,0)) +
  xlab("") + ylab("Mean Score") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  geom_text(aes(label = paste0(estimate, "" ), y = estimate),
            vjust = 4, size = 3, color = "white") +
  stat_compare_means(data=public.goods, 
                     aes(x=elite_t10, y=edu_sat_dummy),
                     method="t.test",
                     vjust = 2,
                     hjust = .1,
                     na.rm = T,
                     size=4)
ggsave("Figure A-13(a).pdf", width = 1.5, height = 1.25, dpi=300, scale = 2)

##########
# Satisfaction with healthcare 
##########

health_satisfaction <-
  public.goods %>% group_by(elite_t10) %>%
  do(tidy(lm_robust(health_sat_dummy ~ 1, data = .))) %>%
  mutate(health_sat_dummy = estimate) %>%
  filter(elite_t10!="NA")

health_satisfaction <- round_df(health_satisfaction, 2)

ggplot(health_satisfaction, aes(x=elite_t10, y=health_sat_dummy)) + 
  geom_bar(stat="identity", width=0.8, fill=c('gray45','gray69'),
           position=position_dodge()) +
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high), width=.2,
                position=position_dodge(.9)) +
  scale_y_continuous(expand = c(0,0)) +
  xlab("") + ylab("Mean Score") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  geom_text(aes(label = paste0(estimate, "" ), y = estimate),
            vjust = 4, size = 3, color = "white") +
  stat_compare_means(data=public.goods, 
                     aes(x=elite_t10, y=health_sat_dummy),
                     method="t.test",
                     vjust = 2,
                     hjust = .1,
                     na.rm = T,
                     size=4)

ggsave("Figure A-13(b).pdf", width = 1.5, height = 1.25, dpi=300, scale = 2)


#################
# Satisfaction with infrastructure
#################

infra_satisfaction <-
  public.goods %>% group_by(elite_t10) %>%
  do(tidy(lm_robust(infr_sat_dummy ~ 1, data = .))) %>%
  mutate(infr_sat_dummy = estimate) %>%
  filter(elite_t10!="NA")

infra_satisfaction <- round_df(infra_satisfaction, 2)

ggplot(infra_satisfaction, aes(x=elite_t10, y=infr_sat_dummy)) + 
  geom_bar(stat="identity", width=0.8, fill=c('gray45','gray69'),
           position=position_dodge()) +
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high), width=.2,
                position=position_dodge(.9)) +
  scale_y_continuous(expand = c(0,0)) +
  xlab("") + ylab("Mean Score") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  geom_text(aes(label = paste0(estimate, "" ), y = estimate),
            vjust = 4, size = 3, color = "white") +
  stat_compare_means(data=public.goods, 
                     aes(x=elite_t10, y=infr_sat_dummy),
                     method="t.test",
                     vjust = 2,
                     hjust = .1,
                     na.rm = T,
                     size=4)
ggsave("Figure A-13(c).pdf", width = 1.5, height = 1.25, dpi=300, scale = 2)

####################
# Satisfaction with environment
####################

environ_satisfaction <-
  public.goods %>% group_by(elite_t10) %>%
  do(tidy(lm_robust(environ_sat_dummy ~ 1, data = .))) %>%
  mutate(environ_sat_dummy = estimate) %>%
  filter(elite_t10!="NA")

environ_satisfaction <- round_df(environ_satisfaction, 2)

ggplot(environ_satisfaction, aes(x=elite_t10, y=environ_sat_dummy)) + 
  geom_bar(stat="identity", width=0.8, fill=c('gray45','gray69'),
           position=position_dodge()) +
  geom_errorbar(aes(ymin=conf.low, ymax=conf.high), width=.2,
                position=position_dodge(.9)) +
  scale_y_continuous(expand = c(0,0)) +
  xlab("") + ylab("Mean Score") +
  theme(plot.title = element_text(hjust = 0.5),
        legend.title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  geom_text(aes(label = paste0(estimate, "" ), y = estimate),
            vjust = 4, size = 3, color = "white") +
  stat_compare_means(data=public.goods, 
                     aes(x=elite_t10, y=environ_sat_dummy),
                     method="t.test",
                     vjust = 2,
                     hjust = .1,
                     na.rm = T,
                     size=4)

ggsave("Figure A-13(d).pdf", width = 1.5, height = 1.25, dpi=300, scale = 2)


